package com.linuxgt.mobile.common.InterfaceList;


import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * Created with IntelliJ IDEA.
 * User: LIUTENG
 * Date: 13-6-19
 * Time: 下午5:47
 * To change this template use File | Settings | File Templates.
 */
public class Weather {
    private static String curl = "http://www.papaonline.com.cn/online/api/mc/product/list/1.json";
    private static String Type = "GET";
    private static String encode="UTF-8";
    private static Logger log = Logger.getLogger(Weather.class);

    public String getResult(){
        String result = "-1";
        URL url = null;
        String str = null;
        OutputStreamWriter os = null;
        BufferedReader is = null;
        HttpURLConnection httpConn = null;
        try{
            url = new URL(curl);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestProperty("Content-Type", "application/json");
            httpConn.setRequestProperty("productCategoryId","50");
            httpConn.setRequestProperty("productStoreId","1");
            httpConn.setRequestProperty("salesChannelEnumId","109");
            httpConn.setRequestProperty("language","zh_CN");
            httpConn.setRequestProperty("eatingStyleEnumId","702");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setRequestMethod(Type);
            os = new OutputStreamWriter(httpConn.getOutputStream(), encode);
            os.flush();
            os.close();
            is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            while ((str = is.readLine()) != null){
                result = str;
            }
            is.close();
            os = null;
            is = null;
            httpConn.disconnect();
        }
        catch (MalformedURLException e){
            result = "malError";
            e.printStackTrace();
        }
        catch (UnknownHostException unhostex){
            log.debug("in requestUrl() UnknownHostException error: "+ unhostex.getMessage());
            result = "unknowError";
        }
        catch (IOException ex){
            log.debug("in requestUrl() error: " + ex.toString());
            result = "ioError";
        }
        finally{
            try{
                if (os != null)
                    os.close();
                if (is != null)
                    is.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
        log.debug("result is [" + result + "]");
        return result;
    }
}
